package org.ctstudio.common.datatype;

import java.time.LocalTime;

import org.ctstudio.common.containers.Range;

public class TimeFragment extends Range<LocalTime> {

  public TimeFragment() {
    this(null, null);
  }

  public TimeFragment(LocalTime from, LocalTime to) {
    super(from, to);
  }

  public long getPeriodSecond() {
    if (this.getFrom() == null || this.getTo() == null || this.getFrom().isAfter(this.getTo())) {
      return 0;
    }
    return this.getTo().toSecondOfDay() - this.getFrom().toSecondOfDay();
  }

  public static long getPeriodSecond(TimeFragment t) {
    return t == null ? 0 : t.getPeriodSecond();
  }

  public LocalTime getPeriod() {
    LocalTime from = this.getFrom() == null ? LocalTime.MIN : this.getFrom();
    LocalTime to = this.getTo() == null ? LocalTime.MAX : this.getTo();
    if (from.isAfter(to)) {
      return LocalTime.MIN;
    } else {
      return to.minusSeconds(from.toSecondOfDay());
    }
  }
}
